package book.manage;

import book.manage.entity.Book;
import book.manage.entity.Borrow;
import book.manage.entity.Student;
import book.manage.sql.SqlUtil;
import lombok.extern.java.Log;
import org.apache.ibatis.io.Resources;

import java.io.IOException;
import java.util.Scanner;
import java.util.logging.LogManager;

/**
 * @author 年年
 * time： 2022/4/29
 */
@Log
public class Main {
    public static void main(String[] args) throws IOException {
        //看得懂为什么要这样写吗-----默认关闭流
        try(Scanner scanner=new Scanner(System.in);) {
            //log日志
            LogManager manager=LogManager.getLogManager();
            manager.readConfiguration(Resources.getResourceAsStream("logging.properties"));

            while (true) {
                System.out.println("------------------");
                System.out.println("1、录入学生信息");
                System.out.println("2、录入书籍信息");
                System.out.println("3、添加借阅信息");
                System.out.print("输入想要执行的操作：输入其他数字退出");
                try{
                    int Input = scanner.nextInt();
                    //清空扫描器
                    scanner.nextLine();
                    switch (Input){
                        case 1:
                            addStudent(scanner);
                            break;
                        case 2:
                            addBooks(scanner);
                            break;
                        case 3:
                            addBorrow(scanner);
                            break;
                        default:
                            System.out.println("用户退出系统");
                            System.exit(0);
                    }
                }catch (Exception e){
                    return;
                }
            }
        }
    }



    //信息的录入
    private static void addStudent(Scanner scanner){
        System.out.print("请输入学生名字：");
        String name= scanner.nextLine();
        System.out.print("请输入学生性别：");
        String sex= scanner.nextLine();
        //偷懒
        if(!(sex.equals("男")||sex.equals("女"))){
            System.out.println("输入错误！只能是男或女，请重新输入：");
            sex=scanner.nextLine();
        }
        System.out.println("请输入学生年级");
        String  grade=scanner.nextLine();
        try {
            int g=Integer.parseInt(grade);    //通过转换字符串
            Student student=new Student(name,sex,g);
            //他这里是一个consumer,我们直接拿bookMapper进行消费，嘎嘎方便 Lambad
            SqlUtil.doSqlwork(bookMapper -> {
               int i= bookMapper.addStudent(student);
               if(i>0) System.out.println("学生信息录入成功!");log.info("新添加了一条学生信息："+student.toString()) ;
            });

        }catch (Exception e){
            e.printStackTrace();
        }



    }

    private static void addBooks(Scanner scanner){
        System.out.println("请输入书籍名称");
        String title =scanner.nextLine();
        System.out.println("请输入书籍介绍");
        String desc =scanner.nextLine();
        System.out.println("请输入书本价格");
        Double price =scanner.nextDouble();
        Book book=new Book(title,desc,price);
        SqlUtil.doSqlwork(bookMapper -> {
            int i= bookMapper.addBook(book);
            if(i>0) System.out.println("书本信息录入成功！");log.info("新添加了一条书籍信息:"+book);
        });
    }

    //借阅信息
    private static void addBorrow(Scanner scanner){
        //处理异常Integer转换的时候可能报错
        try{
            System.out.println("请输入书籍号");
            String a= scanner.nextLine();
            int bid=Integer.parseInt(a);
            System.out.println("请输入学生学号");
            String b= scanner.nextLine();
            int sid=Integer.parseInt(b);

            SqlUtil.doSqlwork(bookMapper -> {
               int i= bookMapper.addBorrow(sid,bid);
                if(i>0) System.out.println("借阅信息录入成功！");log.info("借阅信息：学号："+sid+",书籍号:"+bid);
            });
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    //查询

}
